﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.ToolBoxes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Mvvm.DataBinding;

    public class CreateInstantPatientViewModel : BaseViewModel
    {
        #region Fields

        private readonly ICommand createPatientCommand;
        private readonly IPatientService Service;

        private string firstName;
        private string lastName;

        #endregion Fields

        #region Constructors

        public CreateInstantPatientViewModel(IErrorService errorService, IPatientService service, bool isAsync = true)
            : base(errorService, isAsync)
        {
            this.Service = service;
            this.createPatientCommand = new RelayCommand(this.CreatePatient, this.CanCreatePatient);
        }

        #endregion Constructors

        #region Properties

        public PatientCardDto CreatedPatient
        {
            get; private set;
        }

        public ICommand CreatePatientCommand
        {
            get { return this.createPatientCommand; }
        }

        public string FirstName
        {
            get { return this.firstName; }
            set
            {
                this.firstName = value;
                this.OnPropertyChanged(() => FirstName);
            }
        }

        public string LastName
        {
            get { return this.lastName; }
            set
            {
                this.lastName = value;
                this.OnPropertyChanged(() => LastName);
            }
        }

        #endregion Properties

        #region Methods

        private bool CanCreatePatient()
        {
            return !string.IsNullOrWhiteSpace(this.FirstName)
                && !string.IsNullOrWhiteSpace(this.LastName);
        }

        private void CreatePatient()
        {
            try
            {
                var id = this.Service.Create(new PatientCardDto()
                 {
                     FirstName = this.FirstName,
                     LastName = this.LastName,
                 });
                this.CreatedPatient = this.Service.GetPatient(id);
            }
            catch (Exception ex) { this.ErrorService.HandleError(ex); }
        }

        #endregion Methods
    }
}